package com.buba.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buba.mapper.ProcurementDetailsMapper;
import entity.sparemanage.ProcurementDetails;
import entity.sparemanage.SpareLedger;
import entity.sparemanage.SpareParts;
import com.buba.mapper.SparePartsMapper;
import com.buba.service.SparePartsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import entity.sparemanage.dto.insertPartsDto;
import entity.sparemanage.dto.requisitionDto;
import entity.sparemanage.dto.selAllRequisitionWrapper;
import entity.sparemanage.dto.selByPartsIdDto;
import entity.sparemanage.vo.PeopleVo;
import entity.sparemanage.vo.detialParts;
import entity.sparemanage.vo.selAllRequisitionVo;
import entity.system.CodeRules;
import entity.system.DepartmentManagement;
import entity.system.SupplierManagement;
import feign.systemFeign;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.PageResult;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 备件采购 服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2023-05-18
 */
@Service
public class SparePartsServiceImpl extends ServiceImpl<SparePartsMapper, SpareParts> implements SparePartsService {

    @Autowired
    private SparePartsMapper sparePartsMapper;
    @Autowired
    private systemFeign systemFeign;
    @Autowired
    private ProcurementDetailsMapper procurementDetailsMapper;
    /* *
     * @Author: beisheng
     * @Descriprion: 根据采购单号查询采购信息和采购人员信息，及供应商信息
     * @Date: 2023/5/26 15:41
     * @Return: common.R
     * @Param: [spId]
     */

    @Override
    public selByPartsIdDto selByPartsId(Integer spId) {
        //1.根据采购单号查询采购信息
        selByPartsIdDto selByPartsIdDto = new selByPartsIdDto();
        SpareParts spareParts = sparePartsMapper.selectById(spId);
        BeanUtils.copyProperties(spareParts,selByPartsIdDto);
        //2.查询采购人员信息
        PeopleVo peopleVo = systemFeign.selOnePeople(spareParts.getProcurePersonid());
        selByPartsIdDto.setPeopleVo(peopleVo);
        //3.查询供应商信息
        SupplierManagement supplierManagement = systemFeign.selOneSuppById(spareParts.getProcureSupplierid());
        selByPartsIdDto.setSupplierManagement(supplierManagement);
        //4.返回数据
        return selByPartsIdDto;
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 分页模糊查询全部采购单
     * @Date: 2023/6/6 9:20
     * @Return: common.R
     * @Param: [requisition]
     */

    @Override
    public PageResult selAllRequisition(requisitionDto requisition) {
        List<selAllRequisitionVo> selAllRequisitionVos = new ArrayList<>();
        selAllRequisitionWrapper selAllRequisitionWrapper = new selAllRequisitionWrapper();
        QueryWrapper<SpareParts> queryWrapper = selAllRequisitionWrapper.sparePartsQueryWrapper(requisition);
        Page<SpareParts> page = new Page<>(requisition.getPage(),requisition.getPageSize());
        //1 分页条件查询采购单基本信息
        Page<SpareParts> partsPage = sparePartsMapper.selectPage(page, queryWrapper);
        //1.1 查询总条数
        List<SpareParts> sparePartsList = sparePartsMapper.selectList(new QueryWrapper<SpareParts>().eq("delete_flag", 0));

        List<SpareParts> records = partsPage.getRecords();
        for (SpareParts record : records) {
            selAllRequisitionVo selAllRequisitionVo  = new selAllRequisitionVo();
            BeanUtils.copyProperties(record,selAllRequisitionVo);
            //2.查询采购人员
            PeopleVo peopleVo = systemFeign.selOnePeople(record.getProcurePersonid());
            selAllRequisitionVo.setProcurementPerson(peopleVo);
            //3.查询申请人
            PeopleVo onePeople = systemFeign.selOnePeople(record.getProcureApplicantid());
            selAllRequisitionVo.setApplicant(onePeople);
            //4.查询供应商信息
            SupplierManagement supplierManagement = systemFeign.selOneSuppById(record.getProcureSupplierid());
            selAllRequisitionVo.setSupplierManagement(supplierManagement);
            //5.查询采购部门信息
            DepartmentManagement departmentManagement = systemFeign.selOneDepartment(record.getProcureDepartid());
            selAllRequisitionVo.setDepartmentManagement(departmentManagement);

            selAllRequisitionVos.add(selAllRequisitionVo);
        }
        return new PageResult(requisition.getPage(),requisition.getPageSize(),sparePartsList.size(),selAllRequisitionVos);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertParts(insertPartsDto insertPartsDto) {
        //1 新增备件单
        //1.1 生成备件单号
        //1.查询编码生成规则，进行生成编码
        CodeRules bj = systemFeign.selAllRules("BJCG");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(bj.getDateTime());
        String format = simpleDateFormat.format(new Date());
        List<SpareParts> sparePartsList = sparePartsMapper.selectList(new QueryWrapper<SpareParts>().eq("delete_flag", 0));
        String code = bj.getPrefix()+format+"0"+(sparePartsList.size()+1);
        SpareParts spareParts = new SpareParts();
        spareParts.setProcureCode(code);
        spareParts.setCreateTime(new Date());
        spareParts.setUpdateTime(new Date());
        spareParts.setDeleteFlag(0);
        spareParts.setAuditStatus(1);
        BeanUtils.copyProperties(insertPartsDto,spareParts);
        List<detialParts> ledgerData = insertPartsDto.getLedgerData();
        Integer sum = 0;
        for (detialParts ledgerDatum : ledgerData) {
            sum+=Integer.valueOf(ledgerDatum.getQuantity());
        }
        spareParts.setProcureAllnumber(sum.toString());
        try {
            sparePartsMapper.insert(spareParts);
        }catch (Exception e){
            throw new RuntimeException("新增备件采购失败",e);
        }
        //2填写备件单关联备件明细
        for (detialParts ledgerDatum : ledgerData) {
            ProcurementDetails procurementDetails = new ProcurementDetails();
            procurementDetails.setProcureId(spareParts.getSpId());
            procurementDetails.setSpId(ledgerDatum.getSpId());
            procurementDetails.setProNumber(ledgerDatum.getQuantity());
            procurementDetails.setProPrice(ledgerDatum.getPrice());
            procurementDetails.setProAllmoney(ledgerDatum.getTotal());
            try {
                procurementDetailsMapper.insert(procurementDetails);
            }catch (Exception e){
                throw new RuntimeException("新增备件单关联备件明细失败",e);
            }
        }
    }
}
